import { join } from 'path'
import 'reflect-metadata'

import { createConnection, ConnectionOptions, Connection } from 'typeorm'

import { dbConfig } from '../config/db'

const parentDir = join(__dirname, '..')

const connectionOpts: ConnectionOptions = {
  type: 'mysql',
  host: dbConfig.host,
  port: dbConfig.port,
  username: dbConfig.user,
  password: dbConfig.password,
  database: dbConfig.database,
  entities: [`${parentDir}/entity/**/*.{ts,js}`],
  synchronize: true,
  logging: false,
}

const dbConnection: Promise<Connection> = createConnection(connectionOpts)

export default dbConnection
